// pages/cart/cart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartList: [],
    checkAll: false,
    totalcount: 1,
    totalprice: 1
  },
  /* 获取购物车本地数据 */
  getCartList() {
    var cartList = wx.getStorageSync("cart0717") || []
    if (cartList.length !== 0) {
      this.total()
      var checkAll = cartList.every(ele => {
        return ele.checked === true
      })
      this.setData({
        cartList,
        checkAll
      })
      wx.setStorageSync("checkAll", checkAll)
    }
  },
  /* 减点击事件 */
  setCountMisEvent(e) {
    const { index } = e.currentTarget.dataset
    this.setCount(index, false)
  },
  /* 加点击事件 */
  setCountAddEvent(e) {
    const { index } = e.currentTarget.dataset
    this.setCount(index, true)
  },
  setCount(index, type) {
    var cartList = this.data.cartList;
    if (type) {
      cartList[index].count += 1
    } else {
      if (cartList[index].count > 1) {
        cartList[index].count -= 1
      }
    }
    this.total()
    this.setData({
      cartList
    })
  },
  /* 总价总量计算 */
  total() {
    var totalcount = 0;
    var totalprice = 0;
    this.data.cartList.forEach(ele => {
      if (ele.checked) {
        totalcount += ele.count
        totalprice += ele.count * ele.price
      }
    })
    this.setData({
      totalcount,
      totalprice
    })
  },
  /* 单个选择 */
  checkItem(e) {
    const { index } = e.currentTarget.dataset
    var cartList = this.data.cartList;
    cartList[index].checked = !cartList[index].checked
    var cartList = this.data.cartList;
    var checkAll = cartList.every(ele => {
      return ele.checked === true
    })
    this.setData({
      cartList,
      checkAll
    })
    wx.setStorageSync("checkAll", checkAll)
    wx.setStorageSync("cart0717", cartList)
    this.total()
  },
  /* 全选事件 */
  checkAllEvent() {
    var checkAll = !this.data.checkAll;
    var cartList = this.data.cartList;
    cartList.forEach(ele => {
      ele.checked = checkAll
    })
    this.setData({
      checkAll,
      cartList
    })
    wx.setStorageSync("checkAll", checkAll)
    wx.setStorageSync("cart0717", cartList)
    this.total()
  },
  /* 前往支付 结算按钮 */
  goPayment() {
    /* 将选中的商品推向支付页面 */
    var paymentItems = [];
    this.data.cartList.forEach(ele => {
      if (ele.checked) {
        paymentItems.push(ele)
      }
    })
    wx.navigateTo({
      url: "/pages/payment/payment?paymentItems=" + JSON.stringify(paymentItems)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    /* 获取本地购物车数据 */
    this.getCartList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})